<?php 
if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );
/**
*
* @version $Id$
* @package VirtueMart
* @subpackage html
* @copyright Copyright (C) 2010 Forrest Chamberlain. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
* VirtueMart is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See /administrator/components/com_virtuemart/COPYRIGHT.php for copyright notices and details.
*
* http://virtuemart.net
*/

/**
 * In this page, we display:
 *  - all existing wishlists -- ordered from newest to oldest
 *  OR
 *  - the contents of a wishlist, if a wishlist_id is present
 */
mm_showMyFileName( __FILE__ );


if ($perm->check( "admin,storeadmin" ))
{
	require_once( CLASSPATH . 'ps_wishlist.php' );
	require_once( CLASSPATH . "pageNavigation.class.php" );
	require_once( CLASSPATH . "htmlTools.class.php" );

	$func = JRequest::getInt('func');

	$result = false;
	$result = show_common_items( $limitstart, $limit );
}
else
{
global $mainframe;
	$mainframe->redirect( URL . 'index.php', $GLOBALS['VM_LANG']->_('PHPSHOP_MOD_NO_AUTH') );
}



/* ----------- */


/**
 * Show a list of common wishlist items
 * @author forrest
 * @param int $limitstart
 * @param int $limit
 * @return boolean -- successful or not
 */
function show_common_items( $limitstart, $limit )
{
	global $VM_LANG, $modulename, $sess, $ps_html;

	$count_items = ps_wishlist::get_common_wishlist_items_count( $GLOBALS['keyword'] );
	$num_rows = count( $count_items ) ;
	$product_items = ps_wishlist::get_common_wishlist_items( $GLOBALS['keyword'], $limitstart, $limit );

	if ($product_items !== false)
	{
		// Create the Page Navigation
		$pageNav = new vmPageNav( $num_rows, $limitstart, $limit );

		// Create the List Object with page navigation
		$listObj = new listFactory( $pageNav );

		$listObj->writeSearchHeader($VM_LANG->_('PHPSHOP_WISHLIST_REPORT_LBL'), IMAGEURL."ps_image/wishlist.png", $modulename, "reports");

		// start the list table
		$listObj->startTable();

		// these are the columns in the table
		$columns = Array (
			"#" => "width=\"5%\"", 
			"<input type=\"checkbox\" name=\"toggle\" value=\"\" onclick=\"checkAll(".$num_rows.")\" />" => "width=\"5%\"",
			'ID' => 'width="5%"',
			$VM_LANG->_('PHPSHOP_PRODUCT_LIST_NAME') => 'width="40%"',
			$VM_LANG->_('PHPSHOP_PRODUCT_LIST_SKU') => 'width="30%"',
			$VM_LANG->_('PHPSHOP_WISHLIST_ITEM_COUNT') => 'width="15%"'
		);

		$listObj->writeTableHeader( $columns );

		$i = 0;
		if (count( $product_items ) > 0)
		{
			foreach ($product_items as $p)
			{
				$listObj->newRow();
	
				// The row number
				$listObj->addCell( $pageNav->rowNumber( $i ) );
	
				// The Checkbox
				$listObj->addCell( vmCommonHTML::idBox( $i, $p['product_id'], false, "product_id" ) );
		
				$listObj->addCell( $p['product_id'] );
	
				$url = $_SERVER['PHP_SELF'] . "?pshop_mode=admin&page=product.product_form&product_id=" . $p['product_id'] . '&limitstart=' . $limitstart;
				$listObj->addCell( '<a href="' . $sess->url( $url ) . '">' . $p['product_name'] . '</a><br />' );
	
				$listObj->addCell( $p['product_sku'] );

				$url = $_SERVER['PHP_SELF'] . "?pshop_mode=admin&page=$modulename.item_users&wishlist_prod_id=" . $p['product_id'];
				$listObj->addCell( '<a href="' . $sess->url( $url ) . '">' . $p['item_count'] . ' - View Users</a>' );
		
				$i++;
			}
		}

		$listObj->writeTable();

		$listObj->endTable();

		$listObj->writeFooter( $GLOBALS['keyword'] );

		return true;
	}

	return false;
}

/* ----------- */
/**
 * Show a wishlist -- and its items
 * @author forrest
 * @param array $wl
 * @param int $limitstart
 * @param int $limit
 * @return boolean -- successful or not
 */
function show_wishlist( $wl, $limitstart, $limit )
{
	global $VM_LANG;

	if ($wl !== false)
	{
		$user = ps_wishlist::get_user_info( $wl['user_id'] );

		echo '<h2>' . $wl['name'] . '</h2>';
		echo '<p style="font-style:italic">' . $wl['comments'] . '</p>';
		echo '<p><b>' . $VM_LANG->_('PHPSHOP_WISHLIST_CREATED_LBL') . ':</b> ' . date( 'M d, Y', strtotime( $wl['created'] ) ) . '</p>';
		echo '<p><b>' . $VM_LANG->_('PHPSHOP_WISHLIST_MODIFIED_LBL') . ':</b> ' . date( 'M d, Y', strtotime( $wl['modified'] ) ) . '</p>';
		echo '<p><b>' . $VM_LANG->_('PHPSHOP_WISHLIST_USER_LBL') . ':</b> ' . $user . '</p>';
		echo '<p><b>' . $VM_LANG->_('PHPSHOP_WISHLIST_PUBLIC_LBL') . ':</b> ' . $wl['public'] . '</p>';

		// get this wishlist's items
		$wl_items = ps_wishlist::get_wishlist_items( $wl['wishlist_id'] );

		$wishlist_item_list = '';
		if (count( $wl_items ) > 0)
		{

			echo '<table class="adminlist">';
			echo '<thead>';
			echo '<th></th>';
			echo '<th>' . $VM_LANG->_('PHPSHOP_PRODUCT_NAME_TITLE') . '</th>';
			echo '<th>' . $VM_LANG->_('PHPSHOP_PRODUCT_DESC_TITLE') . '</th>';
			echo '<th>' . $VM_LANG->_('PHPSHOP_CART_QUANTITY') . '</th>';
			echo '</thead>';
			echo '<tbody>';

			foreach ($wl_items as $wl_item) 
			{
				echo '<tr>';
				echo '<td>' . $wl_item['product_thumb_image'] . '</td>';
				echo '<td>' . $wl_item['product_name'] . '</td>';
				echo '<td>' . $wl_item['description'] . $wl_item['product_attributes'] . '</td>';
				echo '<td>' . $wl_item['product_quantity'] . '</td>';
				echo '</tr>';
			}

			echo '</tbody>';
			echo '</table>';
		}
		else
		{
			echo '<p>' . $GLOBALS['VM_LANG']->_('PHPSHOP_WISHLIST_NO_WISHLISTS_ITEMS') . '</p>';
		}

		return true;
	}

	return false;
}
?>
